import requests
from bs4 import BeautifulSoup
import pymysql
import os

if __name__ == '__main__':
    db = pymysql.connect(host="localhost", port=3306, user="root", password="123456", database="test")

    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    sql = 'select * from story order by cate,title'


    cursor.execute(sql)
    results = cursor.fetchall() #用于返回多条数据

    cursor.close()
    db.close()

    f = open('/Users/yanglei/Downloads/story0.txt','w')

    i = 0
    j = 0
    for res in results:
        id = res[0]
        cate = res[1]
        title = res[2]
        content = res[4]
        print(id, cate, title)

        f.write("#### " + cate+"-"+title+"\r\n\r\n")

        f.write(content+"\r\n\r\n")
        i = i + 1
        if(i%1000 == 0):
            j = j + 1
            f = open('/Users/yanglei/Downloads/story'+str(j)+'.txt', 'w')



